<?php

require_once(APPPATH . 'controllers/frontend/layout.php');

class Product extends Layout {

    public function __construct() {
        parent::__construct();
        $this->load->library('pagination');
    }

    public function index() {
        $config['first_link'] = 'Đầu';
        $config['last_link'] = 'Cuối';
        $config['cur_tag_open'] = "<a class='selected'>";
        $config['cur_tag_close'] = "</a>";
        $config['full_tag_open'] = "<div class='page'>";
        $config['full_tag_close'] = '</div>';

        $config['total_rows'] = count($this->mproducts->fetchAll());
        $config['per_page'] = 9;

        //get all the URI segments for pagination and sorting
        $segment_array = $this->uri->segment_array();
        $segment_count = $this->uri->total_segments();

        //for ordering the data items
        $do_orderby = array_search("price", $segment_array);
        if ($do_orderby != "") {
            $orderby = $this->uri->segment($do_orderby + 1);
        } else {
            $orderby = null;
        }

        //getting the records and limit setting
        if (ctype_digit($segment_array[$segment_count])) {
            $this->_data['data']['listpro'] = $this->mproducts->fetchAll($config['per_page'], $segment_array[$segment_count], $orderby);
            array_pop($segment_array);
        } else {
            $this->_data['data']['listpro'] = $this->mproducts->fetchAll($config['per_page'], 0, $orderby);
        }

        $config['base_url'] = base_url(join("/", $segment_array));
        $config['uri_segment'] = count($segment_array) + 1;

        $this->pagination->initialize($config);
        $this->_data['data']['paging'] = $this->pagination->create_links();
        $this->_data['title'] = "Sản phẩm";
        $this->_data['temp'] = "frontend/product/index";
        $this->load->view('frontend/layout/index', $this->_data);
    }

    public function pros($id, $name) {
        $config['first_link'] = 'Đầu';
        $config['last_link'] = 'Cuối';
        $config['cur_tag_open'] = "<a class='selected'>";
        $config['cur_tag_close'] = "</a>";
        $config['full_tag_open'] = "<div class='page'>";
        $config['full_tag_close'] = '</div>';

        $config['per_page'] = 9;
        if ($name == "ccat") {
            $config['total_rows'] = count($this->mproducts->getProByCid($id));
        } elseif ($name == "pcat") {
            $config['total_rows'] = count($this->mproducts->getProByPid($id));
        }

        //get all the URI segments for pagination and sorting
        $segment_array = $this->uri->segment_array();
        $segment_count = $this->uri->total_segments();

        //for ordering the data items
        $do_orderby = array_search("price", $segment_array);
        if ($do_orderby != "") {
            $orderby = $this->uri->segment($do_orderby + 1);
        } else {
            $orderby = null;
        }

        if ($name == "ccat") {
            //getting the records and limit setting
            if (ctype_digit($segment_array[$segment_count])) {
                $this->_data['data']['listpro'] = $this->mproducts->getProByCid($id, $config['per_page'], $segment_array[$segment_count], $orderby);
                array_pop($segment_array);
            } else {
                $this->_data['data']['listpro'] = $this->mproducts->getProByCid($id, $config['per_page'], 0, $orderby);
            }
        } elseif ($name == "pcat") {
            //getting the records and limit setting
            if (ctype_digit($segment_array[$segment_count])) {
                $this->_data['data']['listpro'] = $this->mproducts->getProByPid($id, $config['per_page'], $segment_array[$segment_count], $orderby);
                array_pop($segment_array);
            } else {
                $this->_data['data']['listpro'] = $this->mproducts->getProByPid($id, $config['per_page'], 0, $orderby);
            }
        }

        $config['base_url'] = base_url(join("/", $segment_array));
        $config['uri_segment'] = count($segment_array) + 1;

        $this->pagination->initialize($config);
        $this->_data['data']['paging'] = $this->pagination->create_links();

        $this->_data['data']['cat'] = $this->mcategories->fetchOne($id);
        $this->_data['title'] = $this->_data['data']['cat']->name;
        $this->_data['temp'] = "frontend/product/index";
        $this->load->view('frontend/layout/index', $this->_data);
    }

    public function search() {
        $sear = "";
        if ($this->input->post('btnSearch')) {
            $sear = $this->input->post('txtSearch');
        }
        $config['first_link'] = 'Đầu';
        $config['last_link'] = 'Cuối';
        $config['cur_tag_open'] = "<a class='selected'>";
        $config['cur_tag_close'] = "</a>";
        $config['full_tag_open'] = "<div class='page'>";
        $config['full_tag_close'] = '</div>';

        $config['uri_segment'] = 4;
        $config['base_url'] = base_url() . 'frontend/product/search';
        $config['total_rows'] = count($this->mproducts->getProBySear($sear));
        $config['per_page'] = 9;
        $this->pagination->initialize($config);

        $this->_data['data']['listpro'] = $this->mproducts->getProBySear($sear, $config['per_page'], $this->uri->segment($config['uri_segment']));


        $this->_data['data']['paging'] = $this->pagination->create_links();

        $this->_data['data']['cat'] = (object) array('name' => 'Tìm kiếm: ' . ' ' . $sear);
        $this->_data['title'] = "Tìm kiếm";
        $this->_data['temp'] = "frontend/product/index";
        $this->load->view('frontend/layout/index', $this->_data);
    }

    public function detail($id) {
        $this->_data['data']['img'] = $this->mimages->getByProId($id);
        $this->_data['data']['size'] = $this->msize->getByProId($id);
        $this->_data['data']['bonus'] = $this->mbonus->getByProId($id);
        $this->_data['data']['color'] = $this->mcolor->getByProId($id);
        $this->_data['data']['pro'] = $this->mproducts->fetchOne($id);

        //get all the URI segments
        $segment_array = $this->uri->segment_array();

        $csize = array_search("size", $segment_array);
        if ($csize != "") {
            $size = $this->uri->segment($csize + 1);
            if ($size != 0) {
                $price = $this->msize->fetchOne($size);
                $this->_data['data']['pro']->price = $this->_data['data']['pro']->price + $price->price_bonus;
            }
            $this->_data['data']['get']['size'] = $size;
        } else {
            $size = 0;
        }

        $cbonus = array_search("bonus", $segment_array);
        if ($cbonus != "") {
            $bonus = $this->uri->segment($cbonus + 1);
            if ($bonus != 0) {
                $price = $this->mbonus->fetchOne($bonus);
                $this->_data['data']['pro']->price = $this->_data['data']['pro']->price + $price->price_bonus;
                $this->_data['data']['imgbn'] = $price;
            } else
                $this->_data['data']['imgbn'] = "";
            $this->_data['data']['get']['bonus'] = $bonus;
        } else {
            $bonus = 0;
            $this->_data['data']['imgbn'] = "";
        }

        $ccolor = array_search("color", $segment_array);
        if ($ccolor != "") {
            $color = $this->uri->segment($ccolor + 1);
            if ($color != 0) {
                $price = $this->mcolor->fetchOne($color);
                $this->_data['data']['pro']->price = $this->_data['data']['pro']->price + $price->price_bonus;
                $this->_data['data']['imgcl'] = $price;
                $this->_data['data']['img'] = $this->mimages->getByColorId($color);
            } else
                $this->_data['data']['imgcl'] = "";
            $this->_data['data']['get']['color'] = $color;
        } else {
            $color = 0;
            $this->_data['data']['imgcl'] = "";
        }


        $this->_data['data']['prorelate'] = $this->mproducts->getProRelate($id, $this->_data['data']['pro']->cid, $this->_data['data']['pro']->price);

        $this->_data['title'] = isset($this->_data['data']['pro']->name) ? $this->_data['data']['pro']->name : '';
        $this->_data['temp'] = "frontend/product/detail";
        $this->load->view('frontend/layout/index', $this->_data);
    }

}

//    public function index() {
//        $config['first_link'] = 'Đầu';
//        $config['last_link'] = 'Cuối';
//        $config['cur_tag_open'] = "<a class='selected'>";
//        $config['cur_tag_close'] = "</a>";
//        $config['full_tag_open'] = "<div class='page'>";
//        $config['full_tag_close'] = '</div>';
//
//        $config['uri_segment'] = 4;
//        $config['base_url'] = base_url() . 'frontend/product/index';
//        $config['total_rows'] = count($this->mproducts->fetchAll());
//        $config['per_page'] = 9;
//        $this->pagination->initialize($config);
//
//        $this->_data['data']['listpro'] = $this->mproducts->fetchAll($config['per_page'], $this->uri->segment(4));
//
//        $this->_data['data']['paging'] = $this->pagination->create_links();
//        $this->_data['title'] = "Sản phẩm";
//        $this->_data['temp'] = "frontend/product/index";
//        $this->load->view('frontend/layout/index', $this->_data);
//    }



//    public function pros($name, $id) {
//        $config['first_link'] = 'Đầu';
//        $config['last_link'] = 'Cuối';
//        $config['cur_tag_open'] = "<a class='selected'>";
//        $config['cur_tag_close'] = "</a>";
//        $config['full_tag_open'] = "<div class='page'>";
//        $config['full_tag_close'] = '</div>';
//
//        $config['uri_segment'] = 6;
//        $config['base_url'] = base_url() . 'frontend/product/pros' . '/' . $name . '/' . $id;
//        if ($name == "ccat") {
//            $config['total_rows'] = count($this->mproducts->getProByCid($id));
//        } elseif ($name == "pcat") {
//            $config['total_rows'] = count($this->mproducts->getProByPid($id));
//        }
//
//        $config['per_page'] = 9;
//        $this->pagination->initialize($config);
//
//
//        if ($name == "ccat") {
//            $this->_data['data']['listpro'] = $this->mproducts->getProByCid($id, $config['per_page'], $this->uri->segment($config['uri_segment']));
//            $this->_data['data']['cat'] = $this->mcategories->fetchOne($id);
//            $this->_data['title'] = $this->_data['data']['cat']->name;
//        } elseif ($name == "pcat") {
//            $this->_data['data']['listpro'] = $this->mproducts->getProByPid($id, $config['per_page'], $this->uri->segment($config['uri_segment']));
//            $this->_data['data']['cat'] = $this->mcategories->fetchOne($id);
//            $this->_data['title'] = $this->_data['data']['cat']->name;
//        }
//
//
//        $this->_data['data']['paging'] = $this->pagination->create_links();
//        $this->_data['temp'] = "frontend/product/index";
//        $this->load->view('frontend/layout/index', $this->_data);
//    }